// SlagCar/DailyCheck/DailyCheck.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //出车标记，登记未出车时按钮禁用
    btnDisabled: false
  },
  //修改出车状态
  changeStatus(){
    wx.showModal({
      title: '提示',
      content: '修改车辆今日状态为未出车？',
      confirmText: '确定',
      success: (res) => {
        if(res.confirm) {
          this.submitStatus();
        }
      },
    });
  },
  //提交出车状态
  submitStatus(){
    let post = {
      table: {
        table_id: 'e_cheliangyirisanjianbiao',
        entitys: [{
          details:[],
          entity: {
            row_id: 0,chuchebiaoji: '未出车',chepaihaomayinyong: this.data.driver_car_row_id
          }
        }]
      }},
      url = `DynamicUpdate/dynamic_update_v3`;
    wx.showLoading({mask: true});
    wx.$post(3,url,post,).then(res =>{
      if (res.errcode != 0 )return;
      wx.$toast('修改成功');
      this.getStatus();
    })
  },
  //获取今日出车状态
  getStatus(){
    let url = 'DynamicQuery/query_dynamic_v3',
      post = {
      "oby":"e_cheliangyirisanjianbiao.row_id",
        "table_id":"e_cheliangyirisanjianbiao",
        "page":1,
        "limit":30,
        "SearchWhere":{
          "filters":[
            {"CompareWith":"dbo.FUN_query_add_day(0,e_cheliangyirisanjianbiao.input_time)","Operator":"=","CompareValue":"1","or":0},
            {"CompareWith":"e_cheliangyirisanjianbiao.chepaihaomayinyong","Operator":"=","CompareValue": this.data.driver_car_row_id,"or":0},
            {"CompareWith":"e_cheliangyirisanjianbiao.chuchebiaoji","Operator":"=","CompareValue":"未出车","or":0}
          ],
          "type": 'and'
        },
    };
    wx.showLoading({mask: true})
    this.getCustomData(post).then(data =>{
      wx.hideLoading();
      //查询有数说明已标记未出车
      this.setData({btnDisabled: !!data.length})
    })
  },
  //获取自定义模块数据
  getCustomData(post){
    return new Promise(resolve => {
      let url = `DynamicQuery/query_dynamic_v3`
      wx.$post(3,url,post).then(res =>{
        if (res.errcode != 0) return;
        let data = res.data.row || [];
        resolve(data)

      })
    })
  },

  //开始新的检查
  startNewCheck(){
    wx.showModal({
      title: '提示',
      content: '确认是否开始新的检查？',
      confirmText: '确定',
      success: (res) => {
        if(res.confirm) {
          wx.navigateTo({url: '/SlagCar/DailyCheckDetl/DailyCheckDetl'})
        }
      },
    });
  },
  navTo(e){
    let {url} = e.currentTarget.dataset;
    wx.navigateTo({url})
  },
  //检查
  bindCheck(e){
    let {url,type} = e.currentTarget.dataset,
      checkTableid = '', post = null, pm = null;

    if (type === 'going') checkTableid = 'e_cheliangyirisanjianbiao';//行车中,检查出车前是否已检查
    if (type === 'after') checkTableid = 'e_cheliangyirisanjianbiao_driving'; //收车后，检查行车中是否已检查
    if (type === 'before'){
      wx.navigateTo({url})
    } else {
      let post = {
        "oby": `${checkTableid}.row_id`,
        "table_id": checkTableid,
        "page":1,
        "limit":30,
        "SearchWhere":{
          "filters":[
            {"CompareWith": `dbo.FUN_query_add_day(0,${checkTableid}.input_time)`,"Operator":"=","CompareValue":"1","or":0},
            {"CompareWith": `${checkTableid}.chepaihaomayinyong`,"Operator":"=","CompareValue": this.data.driver_car_row_id,"or":0},
          ],
          "type": 'and'
        },
      }
      this.getCustomData(post).then(data =>{
        if (!data.length) {
          wx.showModal({
            title: '提示',
            content: `请先完成${type === 'going' ? '出车前' : '行车中'}检查`,
            showCancel: false
          })
        }else {
          wx.navigateTo({url})
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let driver_car_row_id = wx.getStorageSync('driver_car_row_id'),
    driver_car_name = wx.getStorageSync('driver_car_name');
    this.setData({driver_car_name,driver_car_row_id})
    this.getStatus()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
